new Vue({
    el: '#app',
    data() {
        return {
            tableData: [],
            headeData: [],
            isTable: true,
            form: {
                page: 1,
                limit: 15,
            },
            loading: false,
            isBatch: false,
            success: 0,
            warning: 0,
            error: 0,
            multipleSelection: [],
        }
    },
    mixins:[importMixin],
    watch: {
        headeData() {
            const timer = setTimeout(() => {
                this.isTable = true;
                clearTimeout(timer)
            })
        }
    },
    methods: {
        chackUpload() {
            const that = this;
            this.loading = true;
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/member/verificationDepartmentOfMember`,
                type: 'POST',
                data: {
                    Data: JSON.stringify(this.tableData)
                },
                success: function (chack) {
                    if (chack.length == 0) {
                        that.submitUpload(0)
                    } else {
                        let main = "";
                        chack.forEach(item => {
                            main += `<span style="margin-right: 10px;color:red;">${item}</span>`;
                        })
                        let html = `<p>部分用户所属部门不存在，是否确认生成？</p>
                        <div style="display: flex;">
                            <span style="font-weight: 700; flex-shrink: 0;">所属部门：</span>
                            <div style="display: flex; flex-wrap: wrap;">${main}</div>
                        </div>`
                        that.$confirm(html, '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            dangerouslyUseHTMLString: true,
                        }).then(() => {
                            $.ajax({
                                headers: requestHeaders(),
                                url: `${BASE_URL2}/member/addDepartment`,
                                type: 'POST',
                                data: {
                                    Data: JSON.stringify(chack),
                                },
                                success: function (res) {
                                    if (res == 'success') {
                                        that.submitUpload(0);
                                    } else {
                                        that.loading = false;
                                        that.$message({
                                            message: "部门添加失败",
                                            duration: 1000,
                                            type: 'error'
                                        });
                                    }
                                },
                                complete: function (XMLHttpRequest) {
                                    requestPretreatment(XMLHttpRequest, false, (res) => {
                                        if (res == 'error') {
                                            that.loading = false;
                                            that.$message({
                                                message: "系统繁忙",
                                                duration: 1000,
                                                type: 'error'
                                            });
                                            return;
                                        }
                                    });
                                },
                                dataType: "text"
                            })
                        }).catch(() => { that.loading = false; });
                    }
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest, false, (res) => {
                        if (res == 'error') {
                            that.loading = false;
                            that.$message({
                                message: "系统繁忙",
                                duration: 1000,
                                type: 'error'
                            });
                            return;
                        }
                    });
                },
                dataType: "json"
            })
        },
        submitUpload(i = 0) {
            const that = this;
            if (i < this.tableData.length) {
                this.loading = true;
                if (i == 0) {
                    this.success = 0;
                    this.error = 0;
                    this.warning = 0;
                }
                if (this.tableData[i].isStatus == '3') {
                    that.warning++;
                    that.submitUpload(i + 1);
                } else if (!this.tableData[i]['姓名']) {
                    this.tableData[i].isStatus = '2';
                    this.tableData[i].Cause = '姓名不能空';
                    this.error++;
                    this.submitUpload(i + 1);
                } else if (!this.tableData[i]['手机号']) {
                    this.tableData[i].isStatus = '2';
                    this.tableData[i].Cause = '手机号不能为空';
                    this.error++;
                    this.submitUpload(i + 1);
                } else if (!validatemobile(this.tableData[i]['手机号'])) {
                    this.tableData[i].isStatus = '2';
                    this.tableData[i].Cause = '该手机号不合法！';
                    this.error++;
                    this.submitUpload(i + 1);
                } else {
                    //判断是否有活动
                    $.ajax({
                        headers: requestHeaders(),
                        url: `${BASE_URL2}/generalBusiness/generalQueryByTableName`,
                        type: 'POST',
                        data: {
                            Data: 'business_activity',
                            Condition: "Row_Status <> '1' and Activity_SerialNumber='" + (that.tableData[i]['活动编号']+'').trim() + "'",
                        },
                        success: function (Data) {
                            if (Data.length > 0) {
                                that.submitForm({ Data: JSON.stringify([that.tableData[i]]) }, (res) => {
                                    if (res.Status == 'success') {
                                        that.tableData[i].isStatus = '1';
                                        that.tableData[i].Cause = '导入成功！';
                                        that.success++;
                                    } else {
                                        that.tableData[i].isStatus = '2';
                                        that.tableData[i].Cause = '未知错误！';
                                        that.error++;
                                    }
                                    that.submitUpload(i + 1);
                                })
                            } else {
                                that.tableData[i].isStatus = '2';
                                that.tableData[i].Cause = '当前活动不存在，请先添加活动';
                                that.error++;
                                that.submitUpload(i + 1);
                            }
                        },
                    })
                }
            } else if (i != 0) {
                this.loading = false;
                this.$message({
                    message: "批量导入结束",
                    duration: 1000,
                    type: 'success'
                })
            } else {
                this.loading = false;
                this.$message({
                    message: '请先导入模板后再上传',
                    duration: 1000,
                    type: 'warning'
                })
            }
        },
        submitForm(data, callback) {
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/member/importMember`,
                type: 'POST',
                data,
                success: function (res) {
                    callback(res);
                },
                error: function () {
                    callback({ Result: '失败', Status: 'fail' })
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            })
        },
        handleChange({ raw }) {//导入监听文件
            const that = this;
            var reader = new FileReader();
            reader.readAsBinaryString(raw);
            reader.onload = function (e) {
                var data = e.target.result;
                var wb = XLSX.read(data, {
                    type: 'binary' //以二进制的方式读取
                });
                var sheet = wb.Sheets[wb.SheetNames[0]];//sheet0代表excel表格中的第一页
                var header = XLSX.utils.sheet_to_json(sheet, { header: 1 })[0];//获取
                var json = XLSX.utils.sheet_to_json(sheet);//利用接口实现转换。
                json.forEach(item => {
                    item.isEdit = false;
                    item.isStatus = '';
                    item.Cause = '';
                })
                that.isTable = false;
                that.headeData = header;
                that.tableData = json;
            }
        },
        // selectable(row) {
        //     return row.isStatus == '3';
        // },
        // tableCellStyle({ row }) {
        //     if (row.isStatus == '3') return { background: '#E6A23C', color: '#fff' }
        // },
        handleSizeChange(val) {//方法
            this.form.page = 1;
            this.form.limit = val;
        },
        handleCurrentChange(val) {//换页
            this.form.page = val;
        },
        // handleSelectionChange(val) {//批量选择
        //     this.multipleSelection = val;
        // },
        // handleBatch() {//批量确认
        //     const that = this;
        //     if (this.multipleSelection.length > 0) {
        //         this.$confirm('是否对已选中的数据添加该部门并完成导入?', '提示', {
        //             confirmButtonText: '确定',
        //             cancelButtonText: '取消',
        //             type: 'warning',
        //         }).then(() => {
        //             this.loading = true;
        //             this.isBatch = true;
        //             $.ajax({
        //                 headers: requestHeaders(),
        //                 url: `${BASE_URL2}/member/verificationDepartmentOfMember`,
        //                 type: 'POST',
        //                 data: {
        //                     Data: JSON.stringify(this.multipleSelection)
        //                 },
        //                 success: function (chack) {
        //                     if (chack.length == 1) {
        //                         $.ajax({
        //                             headers: requestHeaders(),
        //                             url: `${BASE_URL2}/member/addDepartment`,
        //                             type: 'POST',
        //                             data: {
        //                                 Data: JSON.stringify(chack),
        //                             },
        //                             success: function (res) {
        //                                 if (res == 'success') {
        //                                     that.submitForm({ Data: JSON.stringify(that.multipleSelection) }, (res) => {
        //                                         if (res.Status == 'success') {
        //                                             that.multipleSelection.forEach(row => {
        //                                                 row.isStatus = '1';
        //                                                 row.Cause = '导入成功！';
        //                                             })
        //                                             that.warning = that.warning - that.multipleSelection.length;
        //                                             that.success = that.success + that.multipleSelection.length;
        //                                         } else {
        //                                             that.multipleSelection.forEach(row => {
        //                                                 row.isStatus = '2';
        //                                                 row.Cause = '未知错误！';
        //                                             })
        //                                             that.warning = that.warning - that.multipleSelection.length;
        //                                             that.error = that.error + that.multipleSelection.length;
        //                                         }
        //                                         that.$refs.multipleTable.clearSelection();
        //                                         that.loading = false;
        //                                         that.isBatch = false;
        //                                     })
        //                                 } else {
        //                                     that.multipleSelection.forEach(row => {
        //                                         row.isStatus = '2';
        //                                         row.Cause = '部门添加失败';
        //                                     })
        //                                     that.warning = that.warning - that.multipleSelection.length;
        //                                     that.error = that.error + that.multipleSelection.length;
        //                                     that.$refs.multipleTable.clearSelection();
        //                                     that.loading = false;
        //                                     that.isBatch = false;
        //                                 }
        //                             },
        //                             complete: function (XMLHttpRequest) {
        //                                 requestPretreatment(XMLHttpRequest);
        //                             },
        //                             dataType: "text"
        //                         })
        //                     } else {
        //                         that.submitForm({ Data: JSON.stringify(that.multipleSelection) }, (res) => {
        //                             if (res.Status == 'success') {
        //                                 that.multipleSelection.forEach(row => {
        //                                     row.isStatus = '1';
        //                                     row.Cause = '导入成功！';
        //                                 })
        //                                 that.warning = that.warning - that.multipleSelection.length;
        //                                 that.success = that.success + that.multipleSelection.length;
        //                             } else {
        //                                 that.multipleSelection.forEach(row => {
        //                                     row.isStatus = '2';
        //                                     row.Cause = '未知错误！';
        //                                 })
        //                                 that.warning = that.warning - that.multipleSelection.length;
        //                                 that.error = that.error + that.multipleSelection.length;
        //                             }
        //                             that.$refs.multipleTable.clearSelection();
        //                             that.loading = false;
        //                             that.isBatch = false;
        //                         })
        //                     }
        //                 },
        //                 complete: function (XMLHttpRequest) {
        //                     requestPretreatment(XMLHttpRequest, false, (res) => {
        //                         that.loading = false;
        //                         that.isBatch = false;
        //                         that.$message({
        //                             message: '系统繁忙!',
        //                             duration: 1000,
        //                             type: 'error'
        //                         });
        //                         if (res == 'error') {
        //                             return;
        //                         }
        //                     });
        //                 },
        //             })
        //         }).catch(() => { });
        //     } else {
        //         this.$message({
        //             message: '当前没有选择待确认数据!',
        //             duration: 1000,
        //             type: 'warning'
        //         })
        //     }
        // },
        handleEdit(row) {//修改当前行
            row.isEdit = !row.isEdit;
        },
        // handleSubmit(row) {//确认
        //     const that = this;
        //     if (row.isStatus == '3') {
        //         if (this.multipleSelection.some(item => item['手机号'] == row['手机号']) && this.isBatch) {
        //             this.$message({
        //                 message: '当前数据正在批量导入!',
        //                 duration: 1000,
        //                 type: 'warning'
        //             })
        //         } else {
        //             this.$confirm('是否要添加该部门并完成导入?', '提示', {
        //                 confirmButtonText: '确定',
        //                 cancelButtonText: '取消',
        //                 type: 'warning',
        //             }).then(() => {
        //                 $.ajax({
        //                     headers: requestHeaders(),
        //                     url: `${BASE_URL2}/member/verificationDepartmentOfMember`,
        //                     type: 'POST',
        //                     data: {
        //                         Data: JSON.stringify([row])
        //                     },
        //                     success: function (chack) {
        //                         if (chack.length == 1) {
        //                             $.ajax({
        //                                 headers: requestHeaders(),
        //                                 url: `${BASE_URL2}/member/addDepartment`,
        //                                 type: 'POST',
        //                                 data: {
        //                                     Data: JSON.stringify(chack),
        //                                 },
        //                                 success: function (res) {
        //                                     if (res == 'success') {
        //                                         that.submitForm({ Data: JSON.stringify([row]) }, (res) => {
        //                                             if (res.Status == 'success') {
        //                                                 row.isStatus = '1';
        //                                                 row.Cause = '导入成功！';
        //                                                 that.warning--;
        //                                                 that.success++;
        //                                             } else {
        //                                                 row.isStatus = '2';
        //                                                 row.Cause = '未知错误！';
        //                                                 that.warning--;
        //                                                 that.error++;
        //                                             }
        //                                         })
        //                                     } else {
        //                                         row.isStatus = '2';
        //                                         row.Cause = '部门添加失败';
        //                                         that.warning--;
        //                                         that.error++;
        //                                     }
        //                                 },
        //                                 complete: function (XMLHttpRequest) {
        //                                     requestPretreatment(XMLHttpRequest);
        //                                 },
        //                                 dataType: "text"
        //                             })
        //                         } else {
        //                             that.submitForm({ Data: JSON.stringify([row]) }, (res) => {
        //                                 if (res.Status == 'success') {
        //                                     row.isStatus = '1';
        //                                     row.Cause = '导入成功！';
        //                                     that.warning--;
        //                                     that.success++;
        //                                 } else {
        //                                     row.isStatus = '2';
        //                                     row.Cause = '未知错误！';
        //                                     that.warning--;
        //                                     that.error++;
        //                                 }
        //                             })
        //                         }
        //                     },
        //                     complete: function (XMLHttpRequest) {
        //                         requestPretreatment(XMLHttpRequest, false, (res) => {
        //                             that.loading = false;
        //                             that.$message({
        //                                 message: '系统繁忙!',
        //                                 duration: 1000,
        //                                 type: 'error'
        //                             });
        //                             if (res == 'error') {
        //                                 return;
        //                             }
        //                         });
        //                     },
        //                 })
        //             }).catch(() => { });
        //         }
        //     }
        // },
        handleRemove(index) {//移除当前行
            this.$confirm('是否要删除此行?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                this.tableData = this.tableData.filter((i, id) => id != (this.form.page - 1) * this.form.limit + index)
            }).catch(() => { });
        },
        handleClean() {//清空
            this.$confirm('是否要清空导入数据?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                this.tableData = []
            }).catch(() => { });
        },
    },
})